<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-input v-model="listQuery.name" style="width: 200px;" class="filter-item" placeholder="角色名称" @keyup.enter.native="handleFilter"/>
      <el-input v-model="listQuery.role" style="width: 200px;" class="filter-item" placeholder="角色标识" @keyup.enter.native="handleFilter"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button> -->
      <addPage @getLists="getLists"/>
    </div>

    <el-table v-loading="listLoading" :key="tableKey" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column align="center" label="ID">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('authority.role_name')" >
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('authority.role_mark')" >
        <template slot-scope="scope">
          <span>{{ scope.row.role }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('table.operation')" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <editPage :row_data="scope.row" style="display: inline-block;" @getLists="getLists"/>
          <el-button type="danger" icon="el-icon-delete" circle title="删除" class="buttoncss" @click="del(scope.row.id)"/>
          <moveTo :row_data="scope.row.id"/>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import { addPageVisitNum } from '@/utils/addPageVisitNum.js'
import '@/styles/buttonStyle.css'
import { getList, del } from '@/api/shop_admin_auth/shopRole'
import store from '@/store'
import waves from '@/directive/waves' // 水波纹指令
import addPage from './addPage'
import editPage from './editPage'
import moveTo from './moveTo'
export default {
  name: 'ComplexTable',
  directives: {
    waves
  },
  components: { addPage, editPage, moveTo },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined,
        role: undefined
      }
    }
  },
  created() {
    this.getLists()
    addPageVisitNum(this.$router.currentRoute.fullPath, '角色管理')
  },
  methods: {
    getLists() {
      this.listLoading = true
      getList(store.getters.token, this.listQuery).then(response => {
        this.list = response.data.list.data
        this.total = response.data.list.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getLists()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getLists()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getLists()
    },
    del(id) {
      this.$confirm('这一操作将永久删除该记录，你想继续吗?', '警告', {
        confirmButtonText: this.$t('table.confirm'),
        cancelButtonText: this.$t('table.cancel'),
        type: 'warning'
      }).then(() => {
        del(store.getters.token, { id: id }).then(res => {
          if (res.data.status === true) {
            this.$message({
              type: 'success',
              message: '成功删除!'
            })
          } else {
            this.$message({
              message: res.data.msg,
              type: 'error',
              // 报错提示的时间长点
              // 默认为3000
              duration: 4000
            })
          }
          this.getLists()
        })
      })
    }
  }
}
</script>
